<template>
  <div>
    <el-row>
      <el-col :span="3"><el-tag type="success">今日已看诊{{ finish_patient_count }}人</el-tag></el-col>
      <el-col :span="3"><el-tag type="warning">当前有{{ wait_patient_count }}人在排队</el-tag></el-col>
      <el-col :span="18"> </el-col>
    </el-row>
    <el-divider></el-divider>
    <div style="font-size:20px;text-align:left">
      <i class="el-icon-document-checked">患者叫号</i>
    </div>
    <el-divider></el-divider>
    <el-row :gutter="20">
      <el-col :span="6"><el-input v-model="input_patient_id" placeholder="请输入患者病历号"></el-input></el-col>
      <el-col :span="6"><el-input v-model="input_patient_name" placeholder="请输入患者姓名"></el-input></el-col>      
      <el-col :span="2"><el-button @click="getWaitPatient(1)">搜索</el-button></el-col>
      <el-col :span="10"> </el-col>
    </el-row>
    <el-divider></el-divider>
    <el-table
      :data="wait_patient"      style="width: 80%">
      <el-table-column        type="index"     label="编号"        width="80"></el-table-column>
      <el-table-column        prop="real_name"        label="患者姓名"        width="180"></el-table-column>
      <el-table-column        prop="case_number"        label="患者病历号">      </el-table-column>
      <el-table-column label="操作">
      <template slot-scope="scope">
        <el-button   size="mini" @click="createMedicalRecord(scope.$index, scope.row)">创建病历</el-button>
        <el-button size="mini" @click="showMsg()">跳过</el-button>
        <el-button size="mini" @click="showMsg()">叫号</el-button>
      </template>
    </el-table-column>
    </el-table>
    <el-divider></el-divider>
    <el-pagination
    :page-size="pageSize" :total="totalCount" @current-change="wait_patient_table_change"
    layout="prev, pager, next">
  </el-pagination>
  </div>
</template>
<script>
import {getSessionLoginUser} from '../../util/util'
export default {
  data() {
    return {
      input_patient_id:'',
      input_patient_name:'',
      pageSize:10,//每页显示行数
      totalCount:0,
      finish_patient_count:0,//已经看诊完成人数
      wait_patient_count:0,//等待看诊人数
      employee_id:1,//当前医生的id
      wait_patient: []
    }
  },
  methods:{
    //创建病历
    createMedicalRecord(scope_index,scope_row){
      let patient = {};
      patient["real_name"] = this.wait_patient[scope_index].real_name;
      patient["case_number"] = this.wait_patient[scope_index].case_number;
      patient["id"] = this.wait_patient[scope_index].id;
      patient["gender"] = this.wait_patient[scope_index].gender;
      patient["age"] = this.wait_patient[scope_index].age;
      // console.log("创建病历:"+patient["real_name"]);
      sessionStorage.setItem("record_now_patient",JSON.stringify(patient));
      
      //改变患者状态 TODO
      this.$http.get("http://localhost:8092/treatPatient?id="+patient["id"]).then(
        (resp)=>{
          this.$message({
            message:'患者：'+patient["real_name"]+" "+resp.data.msg+";请到 病例首页 开始诊治该患者",
            type:"success"
          });
          //得到已经看诊完成人数
          this.getFinishPatientCount();
          //得到等待看诊人数
          this.getWaitPatientCount();
          //得到等待看诊患者信息
          this.getWaitPatient(1);
        }
      );
    },
    //等待患者table数据改变时触发的事件
    wait_patient_table_change(nowPageNumber){
      this.getWaitPatient(nowPageNumber);
      console.log("等待患者table数据改变时触发的事件:"+nowPageNumber);
    },
    //得到已经看诊完成人数
    getFinishPatientCount(){
      this.$http.get("http://localhost:8092/getFinishPatientCount?employee_id="+this.employee_id,).then(
        (resp)=>{
          this.finish_patient_count = resp.data;
        }
      )   
    },
    //得到等待看诊人数
    getWaitPatientCount(){
      this.$http.get("http://localhost:8092/getWaitPatientCount?employee_id="+this.employee_id,).then(
        (resp)=>{
          this.wait_patient_count = resp.data;
        }
      )
    },
    //得到等待看诊患者信息
    getWaitPatient(nowPageNumber){
      this.$http.get("http://localhost:8092/getWaitPatient?employee_id="+this.employee_id
        +"&case_number="+this.input_patient_id
        +"&real_name="+this.input_patient_name
        +"&nowPageNumber="+nowPageNumber
        +"&pageSize="+this.pageSize).then(
        (resp)=>{
          this.wait_patient = resp.data.list;
          this.totalCount = resp.data.totalCount;
        }
      )
    },
    //得到登录用户信息
    initLoginUserInfo(){
      let userInfo = getSessionLoginUser();
      this.employee_id = userInfo.id;
      // console.log(this.employee_id);
    },
    //显示信息
    showMsg(){
      this.$message({
        message:"需要 排号 子系统，连接失效",
        type:"error"
      })
    }
  },
  mounted:function(){
    //得到登录用户信息
    this.initLoginUserInfo();  
    //得到已经看诊完成人数
    this.getFinishPatientCount();
    //得到等待看诊人数
    this.getWaitPatientCount();
    //得到等待看诊患者信息
    this.getWaitPatient(1);    
  }
}
</script>